package com.ruoyi.teacher.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.teacher.api.domain.*;
import com.ruoyi.teacher.domain.Teacher;
import com.ruoyi.teacher.domain.TeacherTaskDto;
import com.ruoyi.teacher.mapper.TeacherMapper;
import com.ruoyi.teacher.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 老师中心Service业务层处理
 *
 * @author clid
 * @date 2024-03-23
 */
@Service
public class TeacherServiceImpl implements ITeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisService redisService;
    /**
     * 查询老师中心
     *
     * @param id 老师中心主键
     * @return 老师中心
     */
    @Override
    public Teacher selectTeacherById(Long id) {

        return teacherMapper.selectTeacherById(id);
    }

    /**
     * 查询老师中心列表
     *
     * @param teacher 老师中心
     * @return 老师中心
     */
    @Override
    public List<Teacher> selectTeacherList(Teacher teacher) {
        List<Teacher> teachers = teacherMapper.selectTeacherList(teacher);
        List<TeacherType> teacherTypes = teacherMapper.selectTeacherTypeList();
        List<Teacher> superiorsObjs = teacherMapper.loadSuperiorsObj();
        Map<Long, Teacher> teacherMap = superiorsObjs.stream().collect(Collectors.toMap(t -> t.getId(), t -> t));
        Map<Integer, TeacherType> typeMap = teacherTypes.stream().collect(Collectors.toMap(t -> t.getTeacherType(), t -> t));
        return teachers.stream().map(t -> {
            t.setTeacherTypeObj(typeMap.get(t.getTeacherType()));
            t.setSuperiorsObj(teacherMap.get(t.getSuperiors()));
            return t;
        }).collect(Collectors.toList());
    }

    /**
     * 新增老师中心
     *
     * @param teacher 老师中心
     * @return 结果
     */
    @Override
    public int insertTeacher(Teacher teacher) {
        return teacherMapper.insertTeacher(teacher);
    }

    /**
     * 修改老师中心
     *
     * @param teacher 老师中心
     * @return 结果
     */
    @Override
    public int updateTeacher(Teacher teacher) {
        return teacherMapper.updateTeacher(teacher);
    }

    /**
     * 批量删除老师中心
     *
     * @param ids 需要删除的老师中心主键
     * @return 结果
     */
    @Override
    public int deleteTeacherByIds(Long[] ids) {
        return teacherMapper.deleteTeacherByIds(ids);
    }

    /**
     * 删除老师中心信息
     *
     * @param id 老师中心主键
     * @return 结果
     */
    @Override
    public int deleteTeacherById(Long id) {
        return teacherMapper.deleteTeacherById(id);
    }

    @Override
    public List<TeacherType> selectTeacherTypeList() {
        return teacherMapper.selectTeacherTypeList();
    }

    @Override
    public void userToTeacher(UserToTeacherDto dto) {
        Teacher teacher = BeanUtil.copyProperties(dto, Teacher.class);

        teacherMapper.insertTeacher(teacher);
    }

    @Override
    public List<TeacherDto> loadTeachers() {
        List<Teacher> teachers = teacherMapper.selectTeacherList(new Teacher());
        return BeanUtil.copyToList(teachers, TeacherDto.class);
    }

    @Override
    public List<Teacher> loadSuperiorsObj() {
        return teacherMapper.loadSuperiorsObj();
    }

    @Override
    public void importTeacher(List<Teacher> teachers) {
        List<Campus> campuses = teacherMapper.loadCampus();
        Map<String, String> campusMap = campuses.stream().collect(Collectors.toMap(Campus::getCampusName, c -> String.valueOf(c.getId())));
        List<Subject> subjects = teacherMapper.loadSubjects();
        Map<String, String> subjectMap = subjects.stream().collect(Collectors.toMap(Subject::getName, s -> String.valueOf(s.getId())));
        List<Teacher> teacherList = teachers.stream().map(t -> {
            t.setCampusId(campusMap.get(t.getCampus()));
            t.setSubjectId(subjectMap.get(t.getSubject()));
            return t;
        }).collect(Collectors.toList());
        teacherMapper.batchInsert(teacherList);
    }

    /**
     * 首页数据展示
     *
     * @return {@link Map}<{@link String}, {@link Object}>
     */
    @Override
    public Map<String, Object> loadIndexData() {
        HashMap<String, Object> map = new HashMap<>();
        //获取老师任务总数量
        Object teacherCount = redisTemplate.opsForValue().get("Teacher-Task-Count");
        Set<ZSetOperations.TypedTuple<Object>> tuples = redisTemplate.opsForZSet().rangeWithScores("Teacher-Task-Ranking", 0, -1);
       //获取老师完成任务排行榜
        List<String> teacherRanking = tuples.stream().map(t -> {
            int score = Objects.requireNonNull(t.getScore()).intValue();
            String value = Objects.requireNonNull(t.getValue()).toString().split("-")[1];
            return value + "-" + score+"人";
        }).collect(Collectors.toList());
        Collections.reverse(teacherRanking);
        //获取班级总数量
        List<Object> classList = redisService.getCacheList("classList");
        //获取学员数量
        List<Object> studentList = redisService.getCacheList("studentList");
        //待帮扶的数量
        List<Object> seekAssistStudents = studentList.stream().filter(s -> {
            JSONObject jsonObject = JSONUtil.parseObj(s);
            return jsonObject.get("status").toString().equals("2");
        }).collect(Collectors.toList());
        //获取跟踪数量
        Object o = redisTemplate.opsForValue().get("monthly-data");
        //
        map.put("teacherCount", teacherCount);
        map.put("teacherRanking", teacherRanking);
        map.put("classCount", classList.size());
        map.put("studentCount", studentList.size());
        map.put("seekAssistStudents", seekAssistStudents.size());
        return map;
    }
}
